using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;
using System.Xml;
using Newtonsoft.Json.Linq;

namespace BUS
{
	/// <summary> 
	///Author: nnamthach@gmail.com 
	/// <summary>
    public class OpLenhSXControl : BUSControl
    {
		#region Local Variable
        private OpLenhSXDataAccess _objDAO;
		#endregion Local Variable

	    public OpLenhSXControl(string type, string connectString, int timeout = 0)
            : base(type, connectString, timeout) { _objDAO = new OpLenhSXDataAccess(Connnection); }
			
        public OpLenhSXControl(BUSControl common)
            : base(common) { _objDAO = new OpLenhSXDataAccess(common.Connnection); }
	
		#region Method
       		
        public DataTable GetByFilterToDataTable(List<DTO.FilterParameter> filters, ref string sErr, int indexPage = 0, int itemPerPage = 0){
            return _objDAO.GetByFilterToDataTable(filters,ref sErr,indexPage,itemPerPage);
        }
		
		public int GetCountRecord(List<DTO.FilterParameter> filters, ref string sErr){
            return _objDAO.GetCountRecord(filters,ref sErr);
        }
		
		public OpLenhSXInfo[] GetByFilter(List<DTO.FilterParameter> filters, ref string sErr, int indexPage = 0, int itemPerPage = 0){
            OpLenhSXInfo[] res = _objDAO.GetByFilter(filters, ref sErr, indexPage, itemPerPage);
			//get details
			
			return res;
        }
		
        public int Add(OpLenhSXInfo obj, ref string sErr) {
            _objDAO.Add(obj, ref sErr);
			if(sErr == "") {
				//add details
			}
			return 1;
        }
		
        public string Update(OpLenhSXInfo obj) {
            string sErr = _objDAO.Update(obj);
			// delete details
			// add details
			return sErr;
        }
		
        public string Delete(String LenhSX, String Name) {
            string sErr = "";
			// delete details
			
			if(sErr == "") sErr = _objDAO.Delete(LenhSX, Name);
			return sErr;
        }  
        public bool IsExist(String LenhSX, String Name, ref string sErr) {
            return _objDAO.IsExist(LenhSX, Name, ref sErr);
        } 

        public string InsertUpdate(OpLenhSXInfo obj)
        {
            string sErr = "";
            if (IsExist(obj.LenhSX, obj.Name, ref sErr)) { sErr = Update(obj); }
            else { 
				if(sErr =="") Add(obj, ref sErr); 
			}
            return sErr;
        }		
      
		public string TransferIn(DataRow row) {
            OpLenhSXInfo inf = new OpLenhSXInfo(row);
            return InsertUpdate(inf);
        }

        public override string ToXmlString(DTOInfo[] lInf, ref string sErr) {
            string result = "";
            try {
                if (lInf != null && lInf.Length > 0) {
                    XmlDocument doc = new XmlDocument();
                    XmlElement root = doc.CreateElement("OpLenhSX");
                    doc.AppendChild(root);
                    foreach (DTO.DTOInfo x in lInf) {
                        DTO.OpLenhSXInfo it = x as DTO.OpLenhSXInfo;
                        if (it == null) return "";

                        XmlElement row = doc.CreateElement("row");

                        if (it.LenhSX != null) row.SetAttribute("LenhSX", string.Format("{0}", it.LenhSX));
                        if (it.Name != null) row.SetAttribute("Name", string.Format("{0}", it.Name));
						if (it.Status != null) row.SetAttribute("Status", string.Format("{0}", it.Status));
						// details
						//if (it.details != null) row.SetAttribute("details", JsonConvert.SerializeObject(it.details));

                        root.AppendChild(row);
                    }

                    result = doc.InnerXml;
                }
            }
            catch (Exception ex) { sErr = ex.Message; }
            return result;
        }

        public override string TransferIn(DTOInfo inf, BUS.BUSControl.TransferMode mode) {
            string sErr = "";
            if (inf is OpLenhSXInfo) {
                if (mode == BUS.BUSControl.TransferMode.AddNew) Add((DTO.OpLenhSXInfo)inf, ref sErr);
                else if (mode == BUS.BUSControl.TransferMode.Update) sErr = Update((OpLenhSXInfo)inf);
                else sErr = InsertUpdate((OpLenhSXInfo)inf);
            }
            return sErr;
        }

        public override DTO.DTOInfo[] Preview(XmlDocument xml,ref string sErr, string db = null) {
            DTO.DTOInfo[] lRes = null;
            XmlElement doc = xml.DocumentElement;
            lRes = new DTOInfo[doc.ChildNodes.Count];

            for (int i = 0; i < doc.ChildNodes.Count; i++) {
                if (sErr != "") return lRes;
                try {
                    XmlElement ele = doc.ChildNodes[i] as XmlElement;
               
                    DTO.OpLenhSXInfo inf = new DTO.OpLenhSXInfo();
					
/* Can not combine this 					inf.<%AccountColumn(Name)%> = db;*/
					if (GetStringAttribute(ele, "LenhSX") != "") inf.LenhSX = GetStringAttribute(ele, "LenhSX");
					if (GetStringAttribute(ele, "Name") != "") inf.Name = GetStringAttribute(ele, "Name");
					if (GetStringAttribute(ele, "Status") != "") inf.Status = GetStringAttribute(ele, "Status");
					// details
					//string details = GetStringAttribute(ele, "details");
                    //inf.details = JsonConvert.DeserializeObject<CsDataAccessGroupInfo.RoleDetail[]>(details);
                    lRes[i] = inf;
                }
                catch(Exception ex) { sErr = ex.Message; }
            }
            return lRes;
        }
		public bool validate(JObject o, bool pkOnly, ref string sErr) {
            bool inputErr = false;
			if (pkOnly) {
                if (o["LenhSX"] == null) { sErr += "Invalid LenhSX. Please check again"; inputErr = true; }
                if (o["Name"] == null) { sErr += "Invalid Name. Please check again"; inputErr = true; }
            } else {
				if (o["LenhSX"] == null) { sErr += "Invalid LenhSX. Please check again"; inputErr = true; }
				if (o["Name"] == null) { sErr += "Invalid Name. Please check again"; inputErr = true; }
				if (o["Status"] != null) { }//Check validate 
			}
			return inputErr;
		}
		public override bool isField(string field) {
            return new DTO.OpLenhSXInfo().isField(field);
        }
		#endregion Method

    }
}
